﻿
#region Using Directives.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Windows.Forms;
using TreservaDeployHelper.Properties;

#endregion

namespace TreservaDeployHelper
{
    public partial class FormMain : Form
    {
        #region Fields.

        private const string _statusBarDefaultText = "File: <No deploy file open for edit>";
        private readonly string _windowText = string.Format("Treserva Deploy Helper - Version {0}", Application.ProductVersion);
        private XDocument _xDocument;
        protected MruStripMenu MruMenu;
        private readonly string _fileList = string.Empty;
        private bool _isSaved = true;
        private string _currentFile = string.Empty;
        private XElement _application;

        #endregion

        #region Constants.

        private const int NumberOfDatabases = 5;
        private const string MainDB = "MainDB";
        const int NumberOfWinServices = 3;

        #endregion

        #region Constructor.

        public FormMain()
        {
            InitializeComponent();

            MruMenu = new MruStripMenu(mnuRecentFiles, OnMruFile, 16);

            _fileList = RecentFilesSettings.Default.RecentFiles;

            ConnectFieldEvents();
        }

        #endregion

        #region New Deploy File.

        private void New()
        {
            if (!string.IsNullOrEmpty(_currentFile) && _isSaved == false)
            {
                string message;

                if (string.IsNullOrEmpty(_currentFile))
                {
                    message = string.Format("There is a new unsaved deploy file!\n\n Do you want to save the changes before creating a new file?");
                }
                else
                {
                    message = string.Format("The current open file ({0}) got pending changes!\n\n Do you want to save the changes before creating a new file?", Path.GetFileName(_currentFile));
                }

                DialogResult result = MessageBox.Show(this, message, _windowText, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
                                

                if (result == DialogResult.Cancel)
                {
                    return;
                }

                if (result == DialogResult.Yes)
                {
                    Save();
                }
            }

            try
            {
                _xDocument = XmlHelper.CreateNewDocument();

                _currentFile = string.Empty;

                CreateBaseElements(_xDocument);

                ProcessXmlValues(false);

                xmlBrowser.XmlText = _xDocument.ToString();

                SetSaveStatus(false);

                EnabledFields(true);

                mnuSaveAs.Enabled = true;

                mnuClose.Enabled = true;

                mnuUndoAllChanges.Enabled = true;

                sslFile.Text = string.Format("File: {0}", "<New unsaved file>");
            }
            catch (Exception ex)
            {
                string message = string.Format("Error while creating a new deploy file \n\n{0}", ex.Message);

                MessageBox.Show(this, message, _windowText, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region Open Deploy File.

        private void Open(string path)
        {
            if (!string.IsNullOrEmpty(_currentFile) && _isSaved == false)
            {
                string message;

                if(string.IsNullOrEmpty(_currentFile))
                {
                    message = string.Format("There is a new unsaved deploy file!\n\n Do you want to save the changes before opening another file?");
                }
                else
                {
                    message = string.Format("The current open file ({0}) got pending changes!\n\n Do you want to save the changes before opening another file?", Path.GetFileName(_currentFile));
                }

                DialogResult result = MessageBox.Show(this, message, _windowText, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);

                if (result == DialogResult.Cancel)
                {
                    return;
                }

                if (result == DialogResult.Yes)
                {
                    Save();
                }
            }

            if(string.IsNullOrEmpty(path))
            {
                DialogResult dialogResult = openFileDialog.ShowDialog(this);

                if (dialogResult == DialogResult.OK)
                {
                    path = openFileDialog.FileName;
                }
                else
                {
                    return;
                }
            }

            try
            {
                _xDocument = XDocument.Load(path);

                _currentFile = path;

                CreateBaseElements(_xDocument);

                ProcessXmlValues(false);

                xmlBrowser.XmlText = _xDocument.ToString();

                MruMenu.AddFile(_currentFile);

                SetSaveStatus(true);

                EnabledFields(true);

                mnuClose.Enabled = true;

                mnuUndoAllChanges.Enabled = true;

                mnuSaveAs.Enabled = true;

                sslFile.Text = string.Format("File: {0}", Functions.ShortenName(path, 150));
            }
            catch (Exception ex)
            {
                string message = string.Format("Error while opening the deploy file \n\n{0}", ex.Message);

                MessageBox.Show(this, message, _windowText, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region Save Deploy File.

        private void Save()
        {
            if (string.IsNullOrEmpty(_currentFile))
            {
                SaveAs();
            }

            try
            {
                ProcessXmlValues(true);

                _xDocument.Save(_currentFile, SaveOptions.None);

                SetSaveStatus(true);

                xmlBrowser.XmlText = _xDocument.ToString();

                string message = string.Format("Changes are sucessfully saved to:\n\n{0}", Path.GetFileName(_currentFile));

                MessageBox.Show(this, message, _windowText, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                string message = string.Format("Error while saving the deploy file \n\n{0}", ex.Message);

                MessageBox.Show(this, message, _windowText, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void SaveAs()
        {
            DialogResult result = saveFileDialog.ShowDialog(this);
            if (result == DialogResult.OK)
            {
                _currentFile = saveFileDialog.FileName;

                MruMenu.AddFile(_currentFile);

                sslFile.Text = string.Format("File: {0}", Functions.ShortenName(_currentFile, 150));

                Save();
            }
        }

        #endregion

        #region Close Deploy File.

        private void CloseFile()
        {
            if (!_isSaved)
            {
                string message;

                if (string.IsNullOrEmpty(_currentFile))
                {
                    message = string.Format("There is a new unsaved deploy file!\n\n Do you want to save the file before closing?");
                }
                else
                {
                    message = string.Format("The current open file ({0}) got pending changes!\n\n Do you want to save the changes before closing the file?", Path.GetFileName(_currentFile));
                }

                DialogResult result = MessageBox.Show(this, message, _windowText, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);

                if (result == DialogResult.Cancel)
                {
                    return;
                }

                if (result == DialogResult.Yes)
                {
                    Save();
                }
            }

            ClearFields();

            sslFile.Text = _statusBarDefaultText;

            SetSaveStatus(true);

            EnabledFields(false);

            _currentFile = string.Empty;

            xmlBrowser.Navigate("about:blank");

            mnuSaveAs.Enabled = false;

            mnuClose.Enabled = false;

            mnuUndoAllChanges.Enabled = false;
        }

        #endregion

        #region Helper Methods.

        private void CreateBaseElements(XDocument xDoc)
        {
            XElement deployServerConfig = xDoc.Element("DeployServerConfig");
            _application = XmlHelper.GetApplicationNode(deployServerConfig);
        }

        private void ProcessXmlValues(bool save)
        {
            EditApplication(save);

            EditGeneral(save);

            EditWebServer(save);

            EditApplicationServer(save);

            EditWinServicesServer(save);

            EditWinServices(save);

            EditDatabases(save);

            EditIntegration(save);

            EditTreservaAdapter(save);

            EditTreservaPocket(save);

            EditInboundServices(save);

            EditOutboundServices(save);
        }

        public void OnSaveStatusChanged(object sender, EventArgs e)
        {
            SetSaveStatus(false);
        }

        private void SetSaveStatus(bool isSaved)
        {
            if (!isSaved)
            {
                Text = string.Format("{0} *", _windowText);
                _isSaved = false;
            }
            else
            {
                Text = _windowText;
                _isSaved = true;
            }

            tsbSave.Enabled = !_isSaved;
            mnuSave.Enabled = !_isSaved;
        }

        private void EnabledFields(bool enable)
        {
            foreach (var groupBox in this.Descendants<GroupBox>())
            {
                groupBox.Enabled = enable;
            }
        }

        private void RestoreFields()
        {
            foreach (var checkBox in this.Descendants<CheckBox>())
            {
                checkBox.Checked = (bool)checkBox.Tag;
            }

            foreach (var textBox in this.Descendants<TextBox>())
            {
                textBox.Text = (string)textBox.Tag;
            }

            foreach (var comboBox in this.Descendants<ComboBox>())
            {
                comboBox.SelectedIndex = (int)comboBox.Tag;
            }
        }

        private void ClearFields()
        {
            foreach (var groupBox in this.Descendants<GroupBox>())
            {
                if (groupBox.Text.Contains("Database:"))
                    groupBox.Text = "Database";
            }

            foreach (var textBox in this.Descendants<TextBox>())
            {
                textBox.Text = string.Empty;
            }

            foreach (var checkBox in this.Descendants<CheckBox>())
            {
                if (checkBox.Text.Contains("Install Win Service:"))
                    checkBox.Text = "Install Win Service";

                checkBox.Checked = false;
            }

            foreach (var comboBox in this.Descendants<ComboBox>())
            {
                comboBox.SelectedIndex = 0;
            }
        }

        private void ConnectFieldEvents()
        {
            foreach (var textBox in this.Descendants<TextBox>())
            {
                textBox.TextChanged += OnSaveStatusChanged;
            }

            foreach (var checkBox in this.Descendants<CheckBox>())
            {
                checkBox.CheckedChanged += OnSaveStatusChanged;
            }

            foreach (var comboBox in this.Descendants<ComboBox>())
            {
                comboBox.SelectedIndexChanged += OnSaveStatusChanged;
            }
        }

        //private void ConnectEventAllFields(Control.ControlCollection controls)
        //{
        //    foreach (Control ctrl in controls)
        //    {
        //        //if (ctrl.GetType() == typeof(GroupBox))
        //        //{
        //        //    ConnectEventAllFields(ctrl.Controls);
        //        //}
        //        //else if (ctrl.GetType() == typeof(TextBox))
        //        //{
        //        //    ctrl.TextChanged += (OnSaveStatusChanged);
        //        //}
        //        else if (ctrl.GetType() == typeof(CheckBox))
        //        {
        //            ((CheckBox)ctrl).CheckedChanged += (OnSaveStatusChanged);
        //        }
        //        else if (ctrl.GetType() == typeof(ComboBox))
        //        {
        //            ((ComboBox)ctrl).SelectedIndexChanged += (OnSaveStatusChanged);
        //        }
        //    }
        //}

        private int GetIndex(ComboBox comboBox, string text)
        {
            int index = comboBox.Items.IndexOf(text);

            if (index > 0)
                return index;
            return 0;
        }

        #endregion

        #region Menu Events.

        private void mnuNew_Click(object sender, EventArgs e)
        {
            New();
        }

        private void mnuOpen_Click(object sender, EventArgs e)
        {
            Open(string.Empty);
        }

        private void mnuSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void mnuSaveAs_Click(object sender, EventArgs e)
        {
            SaveAs();
        }

        private void mnuClose_Click(object sender, EventArgs e)
        {
            CloseFile();
        }

        private void mnuUndoAllChanges_Click(object sender, EventArgs e)
        {
            RestoreFields();
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void mnuApplicationSettings_Click(object sender, EventArgs e)
        {
            tabMain.SelectTab(0);
        }

        private void mnuHostsSettings_Click(object sender, EventArgs e)
        {
            tabMain.SelectTab(1);
        }

        private void mnuWinServicesSettings_Click(object sender, EventArgs e)
        {
            tabMain.SelectTab(2);
        }

        private void mnuDatabasesSettings_Click(object sender, EventArgs e)
        {
            tabMain.SelectTab(3);
        }

        private void mnuIntegrationSettings_Click(object sender, EventArgs e)
        {
            tabMain.SelectTab(4);
        }

        private void mnuIntegration2Settings_Click(object sender, EventArgs e)
        {
            tabMain.SelectTab(5);
        }

        private void mnuConfigViewer_Click(object sender, EventArgs e)
        {
            tabMain.SelectTab(6);
        }

        #endregion

        #region Toolbar Button Events.

        private void tsbNew_Click(object sender, EventArgs e)
        {
            mnuNew.PerformClick();
        }

        private void tsbOpen_Click(object sender, EventArgs e)
        {
            mnuOpen.PerformClick();
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            mnuSave.PerformClick();
        }

        #endregion

        #region Button Events.

        private void btnSourcePath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.Description = "Select Application Source Path";

            DialogResult result = folderBrowserDialog.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                txtSourcePath.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnRootPath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.Description = "Select Application Server Root Path";

            DialogResult result = folderBrowserDialog.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                txtRootPath.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnVirtualDirPath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.Description = "Select Web Server Virtual Directory Path";

            DialogResult result = folderBrowserDialog.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                txtVirtualDirPath.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnRelativePath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.Description = "Select Application Relative Path";

            DialogResult result = folderBrowserDialog.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                txtRelativePath.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnIntegrationInstallationPath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.Description = "Select Integration Installation Path";

            DialogResult result = folderBrowserDialog.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                txtIntegrationInstallationPath.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnKortpraglingPath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.Description = "Select Kortprägling File Path";

            DialogResult result = folderBrowserDialog.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                txtKortpraglingPath.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnWinServicesRootPath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.Description = "Select Win Services Root Path";

            DialogResult result = folderBrowserDialog.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                txtWinServicesRootPath.Text = folderBrowserDialog.SelectedPath;
            }
        }

        #endregion

        #region Form Events.

        private void FormMain_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(_fileList))
            {
                MruMenu.LoadFromString(_fileList);
                mnuRecentFiles.Enabled = true;
            }

            EnabledFields(false);

            SetSaveStatus(true);
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            RecentFilesSettings.Default.RecentFiles = MruMenu.SaveToString();

            RecentFilesSettings.Default.Save();

            if (!_isSaved)
            {
                string message;

                if (string.IsNullOrEmpty(_currentFile))
                {
                    message = string.Format("There is a new unsaved deploy file!\n\n Do you want to save the file before closing?");
                }
                else
                {
                    message = string.Format("The current open file ({0}) got pending changes!\n\n Do you want to save the changes before closing?", Path.GetFileName(_currentFile));
                }

                DialogResult result = MessageBox.Show(this, message, _windowText, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);

                if (result == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }

                else if (result == DialogResult.Yes)
                {
                    Save();
                    e.Cancel = false;
                }
                else
                {
                    e.Cancel = false;
                }
            }
        }

        private void OnMruFile(int number, String filename)
        {
            if (File.Exists(filename))
            {
                Open(filename);
            }
            else
            {
                string message = string.Format("The file: {0} can not be opened and will be removed from the Recent list(s)", filename);

                MessageBox.Show(this, message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                MruMenu.RemoveFile(number);

                if (MruMenu.MenuItems.Count == 0)
                {
                    mnuRecentFiles.Enabled = false;
                }
            }
        }

        #endregion

        #region EditApplication.

        private void EditApplication(bool save)
        {
            XElement elementApplicationSource = XmlHelper.GetApplicationChildNodes(_application, "ApplicationSource");

            XAttribute attributeName = _application.Attribute("Name");
            if (attributeName == null)
            {
                _application.Add(new XAttribute("Name", ""));
                attributeName = _application.Attribute("Name");
            }

            XAttribute attributeUseLabelInPath = _application.Attribute("UseLabelInPath");
            if (attributeUseLabelInPath == null)
            {
                _application.Add(new XAttribute("UseLabelInPath", "false"));
                attributeUseLabelInPath = _application.Attribute("UseLabelInPath");
            }

            XAttribute attributeVersion = _application.Attribute("Version");
            if (attributeVersion == null)
            {
                _application.Add(new XAttribute("Version", ""));
                attributeVersion = _application.Attribute("Version");
            }

            XAttribute attributeUseLabelFile = elementApplicationSource.Attribute("UseLabelfile");
            if (attributeUseLabelFile == null)
            {
                elementApplicationSource.Add(new XAttribute("UseLabelfile", "false"));
                attributeUseLabelFile = elementApplicationSource.Attribute("UseLabelfile");
            }

            XElement elementSourcePath = elementApplicationSource.Element("SourcePath");
            if (elementSourcePath == null)
            {
                elementApplicationSource.Add(new XElement("SourcePath"));
                elementSourcePath = elementApplicationSource.Element("SourcePath");
            }

            if (attributeName != null)
            {
                if (save)
                {
                    attributeName.Value = txtApplicationName.Text;
                }
                else
                {
                    txtApplicationName.Text = attributeName.Value;
                    txtApplicationName.Tag = txtApplicationName.Text;
                }
            }

            if (attributeVersion != null)
            {
                if (save)
                {
                    attributeVersion.Value = txtApplicationVersion.Text;
                }
                else
                {
                    txtApplicationVersion.Text = attributeVersion.Value;
                    txtApplicationVersion.Tag = txtApplicationVersion.Text;
                }
            }

            if (elementSourcePath != null)
            {
                if (save)
                {
                    elementSourcePath.Value = txtSourcePath.Text;
                }
                else
                {
                    txtSourcePath.Text = elementSourcePath.Value;
                    txtSourcePath.Tag = txtSourcePath.Text;
                }
            }

            if (attributeUseLabelInPath != null)
            {
                if (save)
                {
                    attributeUseLabelInPath.Value = chkUseLabelPath.Checked.ToString().ToLower();
                }
                else
                {
                    chkUseLabelPath.Checked = attributeUseLabelInPath.Value == "true";
                    chkUseLabelPath.Tag = chkUseLabelPath.Checked;
                }
            }

            if (attributeUseLabelFile != null)
            {
                if (save)
                {
                    attributeUseLabelFile.Value = chkUseLabelFile.Checked.ToString().ToLower();
                }
                else
                {
                    chkUseLabelFile.Checked = attributeUseLabelFile.Value == "true";
                    chkUseLabelFile.Tag = chkUseLabelFile.Checked;
                }
            }
        }

        #endregion

        #region EditGeneral.

        private void EditGeneral(bool save)
        {
            XElement elementRelativePath = _application.Element("RelativePath");
            if (elementRelativePath == null)
            {
                _application.AddFirst(new XElement("RelativePath"));
                elementRelativePath = _application.Element("RelativePath");
            }

            XElement elementSendSharePoint = _application.Element("SendSharePoint");
            if (elementSendSharePoint == null)
            {
                _application.AddFirst(new XElement("SendSharePoint"));
                elementSendSharePoint = _application.Element("SendSharePoint");
            }

            XElement elementRemovePreviousInstance = _application.Element("RemovePreviousInstances");
            if (elementRemovePreviousInstance == null)
            {
                _application.AddFirst(new XElement("RemovePreviousInstances"));
                elementRemovePreviousInstance = _application.Element("RemovePreviousInstances");
            }

            XElement elementStopIisWhenInstall = _application.Element("StopIISwhenInstall");
            if (elementStopIisWhenInstall == null)
            {
                _application.AddFirst(new XElement("StopIISwhenInstall"));
                elementStopIisWhenInstall = _application.Element("StopIISwhenInstall");
            }

            XElement elementInstallEnterpriseLog = _application.Element("InstallEnterpriseLog");
            if (elementInstallEnterpriseLog == null)
            {
                _application.AddFirst(new XElement("InstallEnterpriseLog"));
                elementInstallEnterpriseLog = _application.Element("InstallEnterpriseLog");
            }

            XElement elementInstallFileLog = _application.Element("InstallFileLog");
            if (elementInstallFileLog == null)
            {
                _application.AddFirst(new XElement("InstallFileLog"));
                elementInstallFileLog = _application.Element("InstallFileLog");
            }

            XElement elementNcslm = _application.Element("NCSLM");
            if (elementNcslm == null)
            {
                _application.AddFirst(new XElement("NCSLM"));
                elementNcslm = _application.Element("NCSLM");
            }

            if (elementRelativePath != null)
            {
                if (save)
                {
                    elementRelativePath.Value = txtRelativePath.Text;
                }
                else
                {
                    txtRelativePath.Text = elementRelativePath.Value;
                    txtRelativePath.Tag = txtRelativePath.Text;
                }
            }

            if (elementSendSharePoint != null)
            {
                if (save)
                {
                    elementSendSharePoint.Value = chkSendSharePoint.Checked.ToString().ToLower();
                }
                else
                {
                    chkSendSharePoint.Checked = elementSendSharePoint.Value == "true";
                    chkSendSharePoint.Tag = chkSendSharePoint.Checked;
                }
            }

            if (elementRemovePreviousInstance != null)
            {
                if (save)
                {
                    elementRemovePreviousInstance.Value = chkRemovePrevoiusInstance.Checked.ToString().ToLower();
                }
                else
                {
                    chkRemovePrevoiusInstance.Checked = elementRemovePreviousInstance.Value == "true";
                    chkRemovePrevoiusInstance.Tag = chkRemovePrevoiusInstance.Checked;
                }
            }

            if (elementStopIisWhenInstall != null)
            {
                if (save)
                {
                    elementStopIisWhenInstall.Value = chkStopIis.Checked.ToString().ToLower();
                }
                else
                {
                    chkStopIis.Checked = elementStopIisWhenInstall.Value == "true";
                    chkStopIis.Tag = chkStopIis.Checked;
                }
            }

            if (elementInstallEnterpriseLog != null)
            {
                if (save)
                {
                    elementInstallEnterpriseLog.Value = chkInstallEntepriseLog.Checked.ToString().ToLower();
                }
                else
                {
                    chkInstallEntepriseLog.Checked = elementInstallEnterpriseLog.Value == "true";
                    chkInstallEntepriseLog.Tag = chkInstallEntepriseLog.Checked;
                }
            }

            if (elementInstallFileLog != null)
            {
                if (save)
                {
                    elementInstallFileLog.Value = chkInstallFileLog.Checked.ToString().ToLower();
                }
                else
                {
                    chkInstallFileLog.Checked = elementInstallFileLog.Value == "true";
                    chkInstallFileLog.Tag = chkInstallFileLog.Checked;
                }
            }

            if (elementNcslm != null)
            {
                if (save)
                {
                    elementNcslm.Value = chkNcslm.Checked.ToString().ToLower();
                }
                else
                {
                    chkNcslm.Checked = elementNcslm.Value == "true";
                    chkNcslm.Tag = chkNcslm.Checked;
                }
            }
        }

        #endregion

        #region EditApplicationServer.

        private void EditApplicationServer(bool save)
        {
            XElement elementApplicationHosts = XmlHelper.GetApplicationChildNodes(_application, "ApplicationHosts");
            XElement elementApplicationServer = XmlHelper.GetApplicationHostChildNodes(elementApplicationHosts, "ApplicationServer");
            XElement elementRemoteServerIIS = XmlHelper.GetApplicationServerChildNodes(elementApplicationServer, "RemoteServerIIS");

            XElement elementMachineName = elementApplicationServer.Element("MachineName");
            if (elementMachineName == null)
            {
                elementApplicationServer.AddFirst(new XElement("MachineName"));
                elementMachineName = elementApplicationServer.Element("MachineName");
            }

            XElement elementClientCaption = elementApplicationServer.Element("ClientCaption");
            XAttribute attributeUpdate = null;
            if (elementClientCaption == null)
            {
                elementApplicationServer.AddFirst(new XElement("ClientCaption", new XAttribute("Update", "true")));
                elementClientCaption = elementApplicationServer.Element("ClientCaption");

                if (elementClientCaption != null) attributeUpdate = elementClientCaption.Attribute("Update");
            }
            else
            {
                attributeUpdate = elementClientCaption.Attribute("Update");
                if (attributeUpdate == null)
                {
                    elementClientCaption.Add(new XAttribute("Update", "true"));
                }
            }

            XElement elementInstallRootPath = elementApplicationServer.Element("InstallRootPath");
            XAttribute attributeUseRelativePath = null;
            if (elementInstallRootPath == null)
            {
                elementApplicationServer.AddFirst(new XElement("InstallRootPath", new XAttribute("UseRelativePath", "false")));
                elementInstallRootPath = elementApplicationServer.Element("InstallRootPath");

                if (elementInstallRootPath != null) attributeUseRelativePath = elementInstallRootPath.Attribute("UseRelativePath");
            }
            else
            {
                attributeUseRelativePath = elementInstallRootPath.Attribute("UseRelativePath");
                if (attributeUseRelativePath == null)
                {
                    elementInstallRootPath.Add(new XAttribute("UseRelativePath", "false"));
                }
            }

            XElement elementRemoteServerDirName = elementRemoteServerIIS.Element("RemoteServerDirName");
            if (elementRemoteServerDirName == null)
            {
                elementRemoteServerIIS.AddFirst(new XElement("RemoteServerDirName"));
                elementRemoteServerDirName = elementRemoteServerIIS.Element("RemoteServerDirName");
            }

            XElement elementWebAppPoolName = elementRemoteServerIIS.Element("WebAppPoolName");
            if (elementWebAppPoolName == null)
            {
                elementRemoteServerIIS.AddFirst(new XElement("WebAppPoolName"));
                elementWebAppPoolName = elementRemoteServerIIS.Element("WebAppPoolName");
            }

            XElement elementRemoteServerUri = elementRemoteServerIIS.Element("RemoteServerUri");
            XAttribute attributeCreateVirtualDir = null;
            if (elementRemoteServerUri == null)
            {
                elementRemoteServerIIS.Add(new XElement("RemoteServerUri", new XAttribute("CreateVirtualDir", "true")));
                elementRemoteServerUri = elementRemoteServerIIS.Element("RemoteServerUri");

                if (elementRemoteServerUri != null) attributeCreateVirtualDir = elementRemoteServerUri.Attribute("CreateVirtualDir");
            }
            else
            {
                attributeCreateVirtualDir = elementRemoteServerUri.Attribute("CreateVirtualDir");
                if (attributeCreateVirtualDir == null)
                {
                    elementRemoteServerUri.Add(new XAttribute("CreateVirtualDir", "true"));
                }
            }

            if (elementMachineName != null)
            {
                if (save)
                {
                    elementMachineName.Value = txtMachineNameApplicationServer.Text;
                }
                else
                {
                    txtMachineNameApplicationServer.Text = elementMachineName.Value;
                    txtMachineNameApplicationServer.Tag = txtMachineNameApplicationServer.Text;
                }
            }

            if (elementClientCaption != null)
            {
                if (save)
                {
                    elementClientCaption.Value = txtCaption.Text;
                }
                else
                {
                    txtCaption.Text = elementClientCaption.Value;
                    txtCaption.Tag = txtCaption.Text;
                }
            }

            if (elementInstallRootPath != null)
            {
                if (save)
                {
                    elementInstallRootPath.Value = txtRootPath.Text;
                }
                else
                {
                    txtRootPath.Text = elementInstallRootPath.Value;
                    txtRootPath.Tag = txtRootPath.Text;
                }
            }

            if (attributeUpdate != null)
            {
                if (save)
                {
                    attributeUpdate.Value = chkUpdate.Checked.ToString().ToLower();
                }
                else
                {
                    chkUpdate.Checked = attributeUpdate.Value == "true";
                    chkUpdate.Tag = chkUpdate.Checked;
                }
            }

            if (attributeUseRelativePath != null)
            {
                if (save)
                {
                    attributeUseRelativePath.Value = chkUseRelativePathApplicationServer.Checked.ToString().ToLower();
                }
                else
                {
                    chkUseRelativePathApplicationServer.Checked = attributeUseRelativePath.Value == "true";
                    chkUseRelativePathApplicationServer.Tag = chkUseRelativePathApplicationServer.Checked;
                }
            }

            if (elementRemoteServerDirName != null)
            {
                if (save)
                {
                    elementRemoteServerDirName.Value = txtRemoteServerDirName.Text;
                }
                else
                {
                    txtRemoteServerDirName.Text = elementRemoteServerDirName.Value;
                    txtRemoteServerDirName.Tag = txtRemoteServerDirName.Text;
                }
            }

            if (elementWebAppPoolName != null)
            {
                if (save)
                {
                    elementWebAppPoolName.Value = txtWebAppPoolNameRemoteServer.Text;
                }
                else
                {
                    txtWebAppPoolNameRemoteServer.Text = elementWebAppPoolName.Value;
                    txtWebAppPoolNameRemoteServer.Tag = txtWebAppPoolNameRemoteServer.Text;
                }
            }

            if (elementRemoteServerUri != null)
            {
                if (save)
                {
                    elementRemoteServerUri.Value = txtUri.Text;
                }
                else
                {
                    txtUri.Text = elementRemoteServerUri.Value;
                    txtUri.Tag = txtUri.Text;
                }
            }

            if (attributeCreateVirtualDir != null)
            {
                if (save)
                {
                    attributeCreateVirtualDir.Value = chkCreateVirtualDirRemoteServer.Checked.ToString().ToLower();
                }
                else
                {
                    chkCreateVirtualDirRemoteServer.Checked = attributeCreateVirtualDir.Value == "true";
                    chkCreateVirtualDirRemoteServer.Tag = chkCreateVirtualDirRemoteServer.Checked;
                }
            }
        }

        #endregion

        #region EditWebServer.

        private void EditWebServer(bool save)
        {
            XElement elementApplicationHost = XmlHelper.GetApplicationChildNodes(_application, "ApplicationHosts");
            XElement elementWebServer = XmlHelper.GetApplicationHostChildNodes(elementApplicationHost, "WebServer");
            XElement elementWebAppsIis = XmlHelper.GetWebServerChildNodes(elementWebServer, "WebAppsIIS");

            XElement elementMachineName = elementWebServer.Element("MachineName");
            if (elementMachineName == null)
            {
                elementWebServer.AddFirst(new XElement("MachineName"));
                elementMachineName = elementWebServer.Element("MachineName");
            }

            XElement elementUseSsl = elementWebAppsIis.Element("UseSSL");
            if (elementUseSsl == null)
            {
                elementWebAppsIis.AddFirst(new XElement("UseSSL"));
                elementUseSsl = elementWebAppsIis.Element("UseSSL");
            }

            XElement elementWebAppPoolName = elementWebAppsIis.Element("WebAppPoolName");
            if (elementWebAppPoolName == null)
            {
                elementWebAppsIis.AddFirst(new XElement("WebAppPoolName"));
                elementWebAppPoolName = elementWebAppsIis.Element("WebAppPoolName");
            }

            XElement elementVirtualDir = elementWebAppsIis.Element("VirtualDir");
            XAttribute attributeCreateVirtualDir = null;
            if (elementVirtualDir == null)
            {
                elementWebAppsIis.Add(new XElement("VirtualDir", new XAttribute("CreateVirtualDir", "true")));
                elementVirtualDir = elementWebAppsIis.Element("VirtualDir");

                if (elementVirtualDir != null) attributeCreateVirtualDir = elementVirtualDir.Attribute("CreateVirtualDir");

            }
            else
            {
                attributeCreateVirtualDir = elementVirtualDir.Attribute("CreateVirtualDir");
                if (attributeCreateVirtualDir == null)
                {
                    elementVirtualDir.Add(new XAttribute("CreateVirtualDir", "true"));
                }
            }

            XElement elementVirtualDirPath = elementWebAppsIis.Element("VirtualDirPath");
            XAttribute attributeUseRelativePath = null;
            if (elementVirtualDirPath == null)
            {
                elementWebAppsIis.Add(new XElement("VirtualDirPath", new XAttribute("UseRelativePath", "true")));
                elementVirtualDirPath = elementWebAppsIis.Element("VirtualDirPath");

                if (elementVirtualDirPath != null) attributeUseRelativePath = elementVirtualDirPath.Attribute("UseRelativePath");

            }
            else
            {
                attributeUseRelativePath = elementVirtualDirPath.Attribute("UseRelativePath");
                if (attributeUseRelativePath == null)
                {
                    elementVirtualDirPath.Add(new XAttribute("UseRelativePath", "true"));
                }
            }

            if (elementMachineName != null)
            {
                if (save)
                {
                    elementMachineName.Value = txtMachineNameWebServer.Text;
                }
                else
                {
                    txtMachineNameWebServer.Text = elementMachineName.Value;
                    txtMachineNameWebServer.Tag = txtMachineNameWebServer.Text;
                }
            }

            if (elementVirtualDir != null)
            {
                if (save)
                {
                    elementVirtualDir.Value = txtVirtualDirWebServer.Text;
                }
                else
                {
                    txtVirtualDirWebServer.Text = elementVirtualDir.Value;
                    txtVirtualDirWebServer.Tag = txtVirtualDirWebServer.Text;
                }
            }

            if (elementVirtualDirPath != null)
            {
                if (save)
                {
                    elementVirtualDirPath.Value = txtVirtualDirPath.Text;
                }
                else
                {
                    txtVirtualDirPath.Text = elementVirtualDirPath.Value;
                    txtVirtualDirPath.Tag = txtVirtualDirPath.Text;
                }
            }

            if (elementWebAppPoolName != null)
            {
                if (save)
                {
                    elementWebAppPoolName.Value = txtWebbAppPoolNameWebServer.Text;
                }
                else
                {
                    txtWebbAppPoolNameWebServer.Text = elementWebAppPoolName.Value;
                    txtWebbAppPoolNameWebServer.Tag = txtWebbAppPoolNameWebServer.Text;
                }
            }

            if (elementUseSsl != null)
            {
                if (save)
                {
                    elementUseSsl.Value = chkUseSsl.Checked.ToString().ToLower();
                }
                else
                {
                    chkUseSsl.Checked = elementUseSsl.Value == "true";
                    chkUseSsl.Tag = chkUseSsl.Checked;
                }
            }

            if (attributeCreateVirtualDir != null)
            {
                if (save)
                {
                    attributeCreateVirtualDir.Value = chkCreateVirtualDirWebServer.Checked.ToString().ToLower();
                }
                else
                {
                    chkCreateVirtualDirWebServer.Checked = attributeCreateVirtualDir.Value == "true";
                    chkCreateVirtualDirWebServer.Tag = chkCreateVirtualDirWebServer.Checked;
                }
            }

            if (attributeUseRelativePath != null)
            {
                if (save)
                {
                    attributeUseRelativePath.Value = chkUseRelativePathWebServer.Checked.ToString().ToLower();
                }
                else
                {
                    chkUseRelativePathWebServer.Checked = attributeUseRelativePath.Value == "true";
                    chkUseRelativePathWebServer.Tag = chkUseRelativePathWebServer.Checked;
                }
            }
        }

        #endregion

        #region EditWinServicesServer.

        private void EditWinServicesServer(bool save)
        {
            XElement elementApplicationHost = XmlHelper.GetApplicationChildNodes(_application, "ApplicationHosts");
            XElement elementWinServicesServer = XmlHelper.GetApplicationHostChildNodes(elementApplicationHost, "WinServicesServer");

            XElement elementMachineName = elementWinServicesServer.Element("MachineName");
            if (elementMachineName == null)
            {
                elementWinServicesServer.Add(new XElement("MachineName"));
                elementMachineName = elementWinServicesServer.Element("MachineName");
            }

            XElement elementInstallRootPath = elementWinServicesServer.Element("InstallRootPath");
            if (elementInstallRootPath == null)
            {
                elementWinServicesServer.Add(new XElement("InstallRootPath"));
                elementInstallRootPath = elementWinServicesServer.Element("InstallRootPath");
            }

            if (elementMachineName != null)
            {
                if (save)
                {
                    elementMachineName.Value = txtWinServicesMachineName.Text;
                }
                else
                {
                    txtWinServicesMachineName.Text = elementMachineName.Value;
                    txtWinServicesMachineName.Tag = txtWinServicesMachineName.Text;
                }
            }

            if (elementInstallRootPath != null)
            {
                if (save)
                {
                    elementInstallRootPath.Value = txtWinServicesRootPath.Text;
                }
                else
                {
                    txtWinServicesRootPath.Text = elementInstallRootPath.Value;
                    txtWinServicesRootPath.Tag = txtWinServicesRootPath.Text;
                }
            }
        }

        #endregion

        #region EditWinServices.

        private void EditWinServices(bool save)
        {

            XElement elementWinServices = XmlHelper.GetApplicationChildNodes(_application, "WinServices");
            List<XElement> elementWinService = XmlHelper.GetWinServicesChildNodes(elementWinServices, NumberOfWinServices);

            var attributeName = new XAttribute[NumberOfWinServices];
            var attributeInstall = new XAttribute[NumberOfWinServices];
            var elementDisplayName = new XElement[NumberOfWinServices];
            var elementPort = new XElement[NumberOfWinServices];

            for (int i = 0; i < NumberOfWinServices; i++)
            {
                attributeName[i] = elementWinService[i].Attribute("Name");
                if (attributeName[i] == null)
                {
                    elementWinService[i].Add(new XAttribute("Name", ""));
                    attributeName[i] = elementWinService[i].Attribute("Name");
                }

                attributeInstall[i] = elementWinService[i].Attribute("Install");
                if (attributeInstall[i] == null)
                {
                    elementWinService[i].Add(new XAttribute("Install", "false"));
                    attributeInstall[i] = elementWinService[i].Attribute("Install");
                }

                elementDisplayName[i] = elementWinService[i].Element("DisplayName");
                if (elementDisplayName[i] == null)
                {
                    elementWinService[i].Add(new XElement("DisplayName"));
                    elementDisplayName[i] = elementWinService[i].Element("DisplayName");
                }

                elementPort[i] = elementWinService[i].Element("Port");
                if (elementPort[i] == null)
                {
                    elementWinService[i].Add(new XElement("Port"));
                    elementPort[i] = elementWinService[i].Element("Port");
                }
            }

            if (elementWinService.Count > 0)
            {
                if (save)
                {
                    attributeName[0].Value = txtWinServiceName1.Text;
                    elementDisplayName[0].Value = txtWinServiceDisplayName1.Text;
                    elementPort[0].Value = txtWinServicePort1.Text;
                    attributeInstall[0].Value = chkWinServiceInstall1.Checked.ToString().ToLower();

                }
                else
                {
                    chkWinServiceInstall1.Text = chkWinServiceInstall1.Text + ": " + attributeName[0].Value;
                    txtWinServiceName1.Text = attributeName[0].Value;
                    txtWinServiceDisplayName1.Text = elementDisplayName[0].Value;
                    txtWinServicePort1.Text = elementPort[0].Value;
                    chkWinServiceInstall1.Checked = attributeInstall[0].Value == "true";

                    txtWinServiceName1.Tag = txtWinServiceName1.Text;
                    txtWinServiceDisplayName1.Tag = txtWinServiceDisplayName1.Text;
                    txtWinServicePort1.Tag = txtWinServicePort1.Text;
                    chkWinServiceInstall1.Tag = chkWinServiceInstall1.Checked;
                }
            }

            if (elementWinService.Count > 1)
            {
                if (save)
                {
                    attributeName[1].Value = txtWinServiceName2.Text;
                    elementDisplayName[1].Value = txtWinServiceDisplayName2.Text;
                    elementPort[1].Value = txtWinServicePort2.Text;
                    attributeInstall[1].Value = chkWinServiceInstall2.Checked.ToString().ToLower();
                }
                else
                {
                    chkWinServiceInstall2.Text = chkWinServiceInstall2.Text + ": " + attributeName[1].Value;
                    txtWinServiceName2.Text = attributeName[1].Value;
                    txtWinServiceDisplayName2.Text = elementDisplayName[1].Value;
                    txtWinServicePort2.Text = elementPort[1].Value;
                    chkWinServiceInstall2.Checked = attributeInstall[1].Value == "true";

                    txtWinServiceName2.Tag = txtWinServiceName2.Text;
                    txtWinServiceDisplayName2.Tag = txtWinServiceDisplayName2.Text;
                    txtWinServicePort2.Tag = txtWinServicePort2.Text;
                    chkWinServiceInstall2.Tag = chkWinServiceInstall2.Checked;
                }
            }

            if (elementWinService.Count > 2)
            {
                if (save)
                {
                    attributeName[2].Value = txtWinServiceName3.Text;
                    elementDisplayName[2].Value = txtWinServiceDisplayName3.Text;
                    elementPort[2].Value = txtWinServicePort3.Text;
                    attributeInstall[2].Value = chkWinServiceInstall3.Checked.ToString().ToLower();
                }
                else
                {
                    chkWinServiceInstall3.Text = chkWinServiceInstall3.Text + ": " + attributeName[2].Value;
                    txtWinServiceName3.Text = attributeName[2].Value;
                    txtWinServiceDisplayName3.Text = elementDisplayName[2].Value;
                    txtWinServicePort3.Text = elementPort[2].Value;
                    chkWinServiceInstall3.Checked = attributeInstall[2].Value == "true";

                    txtWinServiceName3.Tag = txtWinServiceName3.Text;
                    txtWinServiceDisplayName3.Tag = txtWinServiceDisplayName3.Text;
                    txtWinServicePort3.Tag = txtWinServicePort3.Text;
                    chkWinServiceInstall3.Tag = chkWinServiceInstall3.Checked;
                }
            }
        }

        #endregion

        #region EditDatabases.

        private void EditDatabases(bool save)
        {
            Functions.SortDatabaseNodes(_xDocument, MainDB);
            XElement elementDatabases = XmlHelper.GetApplicationChildNodes(_application, "Databases");
            List<XElement> elementDatabase = XmlHelper.GetDatabasesChildNodes(elementDatabases, NumberOfDatabases);

            var attributeName = new XAttribute[NumberOfDatabases];
            var elementMachineName = new XElement[NumberOfDatabases];
            var elementDataSource = new XElement[NumberOfDatabases];
            var elementUserId = new XElement[NumberOfDatabases];
            var elementPassword = new XElement[NumberOfDatabases];
            var elementDbSystem = new XElement[NumberOfDatabases];
            var elementMaxPoolSize = new XElement[NumberOfDatabases];
            var elementConnectionLifeTime = new XElement[NumberOfDatabases];

            

            for (int i = 0; i < NumberOfDatabases; i++)
            {
                attributeName[i] = elementDatabase[i].Attribute("Name");
                if (attributeName[i] == null)
                {
                    elementDatabase[i].Add(new XAttribute("Name", ""));
                    attributeName[i] = elementDatabase[i].Attribute("Name");
                }

                elementMachineName[i] = elementDatabase[i].Element("MachineName");
                if (elementMachineName[i] == null)
                {
                    elementDatabase[i].Add(new XElement("MachineName"));
                    elementMachineName[i] = elementDatabase[i].Element("MachineName");
                }

                elementDataSource[i] = elementDatabase[i].Element("DataSource");
                if (elementDataSource[i] == null)
                {
                    elementDatabase[i].Add(new XElement("DataSource"));
                    elementDataSource[i] = elementDatabase[i].Element("DataSource");
                }

                elementUserId[i] = elementDatabase[i].Element("UserId");
                if (elementUserId[i] == null)
                {
                    elementDatabase[i].Add(new XElement("UserId"));
                    elementUserId[i] = elementDatabase[i].Element("UserId");
                }

                elementPassword[i] = elementDatabase[i].Element("Pwd");
                if (elementPassword[i] == null)
                {
                    elementDatabase[i].Add(new XElement("Pwd"));
                    elementPassword[i] = elementDatabase[i].Element("Pwd");
                }

                elementDbSystem[i] = elementDatabase[i].Element("DbSystem");
                if (elementDbSystem[i] == null)
                {
                    elementDatabase[i].Add(new XElement("DbSystem"));
                    elementDbSystem[i] = elementDatabase[i].Element("DbSystem");
                }

                if (i == 0)
                {
                    elementMaxPoolSize[i] = elementDatabase[i].Element("MaxPoolSize");
                    if (elementMaxPoolSize[i] == null)
                    {
                        elementDatabase[i].Add(new XElement("MaxPoolSize"));
                        elementMaxPoolSize[i] = elementDatabase[i].Element("MaxPoolSize");
                    }

                    elementConnectionLifeTime[i] = elementDatabase[i].Element("ConnectionLifetime");
                    if (elementConnectionLifeTime[i] == null)
                    {
                        elementDatabase[i].Add(new XElement("ConnectionLifetime"));
                        elementConnectionLifeTime[i] = elementDatabase[i].Element("ConnectionLifetime");
                    }
                }
            }

            

            //elementMaxPoolSize[0] = elementDatabase[0].Element("MaxPoolSize");
            //if (elementMaxPoolSize[0] == null)
            //{
            //    elementDatabase[0].Add(new XElement("MaxPoolSize"));
            //    elementMaxPoolSize[0] = elementDatabase[0].Element("MaxPoolSize");
            //}

            //elementConnectionLifeTime[0] = elementDatabase[0].Element("ConnectionLifetime");
            //if (elementConnectionLifeTime[0] == null)
            //{
            //    elementDatabase[0].Add(new XElement("ConnectionLifetime"));
            //    elementConnectionLifeTime[0] = elementDatabase[0].Element("ConnectionLifetime");
            //}

            if (elementDatabase.Count > 0)
            {
                if (save)
                {
                    elementMachineName[0].Value = txtDbServerName1.Text;
                    elementDataSource[0].Value = txtDbDataSource1.Text;
                    elementUserId[0].Value = txtDbUserId1.Text;
                    elementPassword[0].Value = txtDbPassword1.Text;
                    elementDbSystem[0].Value = cboDbSystem1.SelectedItem.ToString();
                    elementMaxPoolSize[0].Value = txtDbPoolSize1.Text;
                    elementConnectionLifeTime[0].Value = txtDbConnectionLifetime1.Text;
                }
                else
                {
                    grbDatabase1.Text = grbDatabase1.Text + ": " + attributeName[0].Value;
                    txtDbServerName1.Text = elementMachineName[0].Value;
                    txtDbDataSource1.Text = elementDataSource[0].Value;
                    txtDbUserId1.Text = elementUserId[0].Value;
                    txtDbPassword1.Text = elementPassword[0].Value;
                    cboDbSystem1.SelectedIndex = GetIndex(cboDbSystem1, elementDbSystem[0].Value);
                    txtDbPoolSize1.Text = elementMaxPoolSize[0].Value;
                    txtDbConnectionLifetime1.Text = elementConnectionLifeTime[0].Value;

                    txtDbServerName1.Tag = txtDbServerName1.Text;
                    txtDbDataSource1.Tag = txtDbDataSource1.Text;
                    txtDbUserId1.Tag = txtDbUserId1.Text;
                    txtDbPassword1.Tag = txtDbPassword1.Text;
                    cboDbSystem1.Tag = GetIndex(cboDbSystem1, elementDbSystem[0].Value);
                    txtDbPoolSize1.Tag = txtDbPoolSize1.Text;
                    txtDbConnectionLifetime1.Tag = txtDbConnectionLifetime1.Text;
                }
            }

            if (elementDatabase.Count > 1)
            {
                if (save)
                {
                    elementMachineName[1].Value = txtDbServerName2.Text;
                    elementDataSource[1].Value = txtDbDataSource2.Text;
                    elementUserId[1].Value = txtDbUserId2.Text;
                    elementPassword[1].Value = txtDbPassword2.Text;
                    elementDbSystem[1].Value = cboDbSystem2.SelectedItem.ToString();
                }
                else
                {
                    grbDatabase2.Text = grbDatabase2.Text + ": " + attributeName[1].Value;
                    txtDbServerName2.Text = elementMachineName[1].Value;
                    txtDbDataSource2.Text = elementDataSource[1].Value;
                    txtDbUserId2.Text = elementUserId[1].Value;
                    txtDbPassword2.Text = elementPassword[1].Value;
                    cboDbSystem2.SelectedIndex = GetIndex(cboDbSystem2, elementDbSystem[1].Value);

                    txtDbServerName2.Tag = txtDbServerName2.Text;
                    txtDbDataSource2.Tag = txtDbDataSource2.Text;
                    txtDbUserId2.Tag = txtDbUserId2.Text;
                    txtDbPassword2.Tag = txtDbPassword2.Text;
                    cboDbSystem2.Tag = GetIndex(cboDbSystem2, elementDbSystem[1].Value);
                }
            }

            if (elementDatabase.Count > 2)
            {
                if (save)
                {
                    elementMachineName[2].Value = txtDbServerName3.Text;
                    elementDataSource[2].Value = txtDbDataSource3.Text;
                    elementUserId[2].Value = txtDbUserId3.Text;
                    elementPassword[2].Value = txtDbPassword3.Text;
                    elementDbSystem[2].Value = cboDbSystem3.SelectedItem.ToString();
                }
                else
                {
                    grbDatabase3.Text = grbDatabase3.Text + ": " + attributeName[2].Value;
                    txtDbServerName3.Text = elementMachineName[2].Value;
                    txtDbDataSource3.Text = elementDataSource[2].Value;
                    txtDbUserId3.Text = elementUserId[2].Value;
                    txtDbPassword3.Text = elementPassword[2].Value;
                    cboDbSystem3.SelectedIndex = GetIndex(cboDbSystem3, elementDbSystem[2].Value);

                    txtDbServerName3.Tag = txtDbServerName3.Text;
                    txtDbDataSource3.Tag = txtDbDataSource3.Text;
                    txtDbUserId3.Tag = txtDbUserId3.Text;
                    txtDbPassword3.Tag = txtDbPassword3.Text;
                    cboDbSystem3.Tag = GetIndex(cboDbSystem3, elementDbSystem[2].Value);
                }
            }

            if (elementDatabase.Count > 3)
            {
                if (save)
                {
                    elementMachineName[3].Value = txtDbServerName4.Text;
                    elementDataSource[3].Value = txtDbDataSource4.Text;
                    elementUserId[3].Value = txtDbUserId4.Text;
                    elementPassword[3].Value = txtDbPassword4.Text;
                    elementDbSystem[3].Value = cboDbSystem4.SelectedItem.ToString();
                }
                else
                {
                    grbDatabase4.Text = grbDatabase4.Text + ": " + attributeName[3].Value;
                    txtDbServerName4.Text = elementMachineName[3].Value;
                    txtDbDataSource4.Text = elementDataSource[3].Value;
                    txtDbUserId4.Text = elementUserId[3].Value;
                    txtDbPassword4.Text = elementPassword[3].Value;
                    cboDbSystem4.SelectedIndex = GetIndex(cboDbSystem4, elementDbSystem[3].Value);

                    txtDbServerName4.Tag = txtDbServerName4.Text;
                    txtDbDataSource4.Tag = txtDbDataSource4.Text;
                    txtDbUserId4.Tag = txtDbUserId4.Text;
                    txtDbPassword4.Tag = txtDbPassword4.Text;
                    cboDbSystem4.Tag = GetIndex(cboDbSystem4, elementDbSystem[3].Value);
                }
            }

            if (elementDatabase.Count > 4)
            {
                if (save)
                {
                    elementMachineName[4].Value = txtDbServerName5.Text;
                    elementDataSource[4].Value = txtDbDataSource5.Text;
                    elementUserId[4].Value = txtDbUserId5.Text;
                    elementPassword[4].Value = txtDbPassword5.Text;
                    elementDbSystem[4].Value = cboDbSystem5.SelectedItem.ToString();
                }
                else
                {
                    grbDatabase5.Text = grbDatabase5.Text + ": " + attributeName[4].Value;
                    txtDbServerName5.Text = elementMachineName[4].Value;
                    txtDbDataSource5.Text = elementDataSource[4].Value;
                    txtDbUserId5.Text = elementUserId[4].Value;
                    txtDbPassword5.Text = elementPassword[4].Value;
                    cboDbSystem5.SelectedIndex = GetIndex(cboDbSystem5, elementDbSystem[4].Value);

                    txtDbServerName5.Tag = txtDbServerName5.Text;
                    txtDbDataSource5.Tag = txtDbDataSource5.Text;
                    txtDbUserId5.Tag = txtDbUserId5.Text;
                    txtDbPassword5.Tag = txtDbPassword5.Text;
                    cboDbSystem5.Tag = GetIndex(cboDbSystem5, elementDbSystem[4].Value);
                }
            }
        }

        #endregion

        #region EditIntegration.

        private void EditIntegration(bool save)
        {
            XElement elementIntegration = XmlHelper.GetApplicationChildNodes(_application, "Integration");

            XAttribute attributeIntegrationInstall = elementIntegration.Attribute("Install");
            if (attributeIntegrationInstall == null)
            {
                elementIntegration.Add(new XAttribute("Install", "false"));
                attributeIntegrationInstall = elementIntegration.Attribute("Install");
            }

            XElement elementIntegrationPath = elementIntegration.Element("IntegrationPath");
            if (elementIntegrationPath == null)
            {
                elementIntegration.Add(new XElement("IntegrationPath"));
                elementIntegrationPath = elementIntegration.Element("IntegrationPath");
            }

            XElement elementInstallEntepriseLog = elementIntegration.Element("InstallEnterpriseLog");
            if (elementInstallEntepriseLog == null)
            {
                elementIntegration.Add(new XElement("InstallEnterpriseLog", "true"));
                elementInstallEntepriseLog = elementIntegration.Element("InstallEnterpriseLog");
            }

            XElement elementInstallFileLog = elementIntegration.Element("InstallFileLog");
            if (elementInstallFileLog == null)
            {
                elementIntegration.Add(new XElement("InstallFileLog", "true"));
                elementInstallFileLog = elementIntegration.Element("InstallFileLog");
            }

            XElement elementInstallMessageLog = elementIntegration.Element("InstallMessageLog");
            if (elementInstallMessageLog == null)
            {
                elementIntegration.Add(new XElement("InstallMessageLog", "true"));
                elementInstallMessageLog = elementIntegration.Element("InstallMessageLog");
            }

            if (attributeIntegrationInstall != null)
            {
                if (save)
                {
                    attributeIntegrationInstall.Value = chkInstallIntegration.Checked.ToString().ToLower();
                }
                else
                {
                    chkInstallIntegration.Checked = attributeIntegrationInstall.Value == "true";
                    chkInstallIntegration.Tag = chkInstallIntegration.Checked;
                }
            }

            if (elementIntegrationPath != null)
            {
                if (save)
                {
                    elementIntegrationPath.Value = txtIntegrationInstallationPath.Text;
                }
                else
                {
                    txtIntegrationInstallationPath.Text = elementIntegrationPath.Value;
                    txtIntegrationInstallationPath.Tag = txtIntegrationInstallationPath.Text;
                }
            }

            if (elementInstallEntepriseLog != null)
            {
                if (save)
                {
                    elementInstallEntepriseLog.Value = chkIntegrationEnterpriseLog.Checked.ToString().ToLower();
                }
                else
                {
                    chkIntegrationEnterpriseLog.Checked = elementInstallEntepriseLog.Value == "true";
                    chkIntegrationEnterpriseLog.Tag = chkIntegrationEnterpriseLog.Checked;
                }
            }

            if (elementInstallFileLog != null)
            {
                if (save)
                {
                    elementInstallFileLog.Value = chkIntegrationFileLog.Checked.ToString().ToLower();
                }
                else
                {
                    chkIntegrationFileLog.Checked = elementInstallFileLog.Value == "true";
                    chkIntegrationFileLog.Tag = chkIntegrationFileLog.Checked;
                }
            }

            if (elementInstallMessageLog != null)
            {
                if (save)
                {
                    elementInstallMessageLog.Value = chkIntegrationMessageLog.Checked.ToString().ToLower();
                }
                else
                {
                    chkIntegrationMessageLog.Checked = elementInstallMessageLog.Value == "true";
                    chkIntegrationMessageLog.Tag = chkIntegrationMessageLog.Checked;
                }
            }
        }

        #endregion

        #region EditTreservaAdapter.

        private void EditTreservaAdapter(bool save)
        {
            XElement elementIntegration = XmlHelper.GetApplicationChildNodes(_application, "Integration");
            XElement elementTreservaAdapter = XmlHelper.GetIntegrationChildNodes(elementIntegration, "TreservaAdapter");

            XElement elementNetBaseHost = elementTreservaAdapter.Element("NetBaseHost");
            if (elementNetBaseHost == null)
            {
                elementTreservaAdapter.Add(new XElement("NetBaseHost"));
                elementNetBaseHost = elementTreservaAdapter.Element("NetBaseHost");
            }

            XElement elementNetBasePort = elementTreservaAdapter.Element("NetBasePort");
            if (elementNetBasePort == null)
            {
                elementTreservaAdapter.Add(new XElement("NetBasePort"));
                elementNetBasePort = elementTreservaAdapter.Element("NetBasePort");
            }

            XElement elementHttpBaseHost = elementTreservaAdapter.Element("HttpBaseHost");
            if (elementHttpBaseHost == null)
            {
                elementTreservaAdapter.Add(new XElement("HttpBaseHost"));
                elementHttpBaseHost = elementTreservaAdapter.Element("HttpBaseHost");
            }

            XElement elementHttpBasePort = elementTreservaAdapter.Element("HttpBasePort");
            if (elementHttpBasePort == null)
            {
                elementTreservaAdapter.Add(new XElement("HttpBasePort"));
                elementHttpBasePort = elementTreservaAdapter.Element("HttpBasePort");
            }

            if (elementNetBaseHost != null)
            {
                if (save)
                {
                    elementNetBaseHost.Value = txtNetBase.Text;
                }
                else
                {
                    txtNetBase.Text = elementNetBaseHost.Value;
                    txtNetBase.Tag = txtNetBase.Text;
                }
            }

            if (elementNetBasePort != null)
            {
                if (save)
                {
                    elementNetBasePort.Value = txtNetBasePort.Text;
                }
                else
                {
                    txtNetBasePort.Text = elementNetBasePort.Value;
                    txtNetBasePort.Tag = txtNetBasePort.Text;
                }
            }

            if (elementHttpBaseHost != null)
            {
                if (save)
                {
                    elementHttpBaseHost.Value = txtHttpBase.Text;
                }
                else
                {
                    txtHttpBase.Text = elementHttpBaseHost.Value;
                    txtHttpBase.Tag = txtHttpBase.Text;
                }
            }

            if (elementHttpBasePort != null)
            {
                if (save)
                {
                    elementHttpBasePort.Value = txtHttpBasePort.Text;
                }
                else
                {
                    txtHttpBasePort.Text = elementHttpBasePort.Value;
                    txtHttpBasePort.Tag = txtHttpBasePort.Text;
                }
            }
        }

        #endregion

        #region EditTreservaPocket

        private void EditTreservaPocket(bool save)
        {
            XElement elementIntegration = XmlHelper.GetApplicationChildNodes(_application, "Integration");
            XElement elementTreservaPocket = XmlHelper.GetIntegrationChildNodes(elementIntegration, "TreservaPocket");

            XAttribute attributeTreservaPocketInstall = elementTreservaPocket.Attribute("Install");
            if (attributeTreservaPocketInstall == null)
            {
                elementTreservaPocket.Add(new XAttribute("Install", "false"));
                attributeTreservaPocketInstall = elementTreservaPocket.Attribute("Install");
            }

            XElement elementTesReportService = elementTreservaPocket.Element("TESReportService");
            if (elementTesReportService == null)
            {
                elementTreservaPocket.Add(new XElement("TESReportService"));
                elementTesReportService = elementTreservaPocket.Element("TESReportService");
            }

            if (attributeTreservaPocketInstall != null)
            {
                if (save)
                {
                    attributeTreservaPocketInstall.Value = chkInstallTreservaPocket.Checked.ToString().ToLower();
                }
                else
                {
                    chkInstallTreservaPocket.Checked = attributeTreservaPocketInstall.Value == "true";
                    chkInstallTreservaPocket.Tag = chkInstallTreservaPocket.Checked;
                }
            }

            if (elementTesReportService != null)
            {
                if (save)
                {
                    elementTesReportService.Value = txtTesReportService.Text;
                }
                else
                {
                    txtTesReportService.Text = elementTesReportService.Value;
                    txtTesReportService.Tag = txtTesReportService.Text;
                }
            }
        }

        #endregion

        #region EditInboundServices.

        private void EditInboundServices(bool save)
        {
            XElement elementIntegration = XmlHelper.GetApplicationChildNodes(_application, "Integration");
            XElement elementIntegrationStarter = XmlHelper.GetIntegrationChildNodes(elementIntegration, "IntegrationStarter");

            #region Cert.

            XElement elementCert = elementIntegrationStarter.Element("Cert");
            if (elementCert == null)
            {
                elementIntegrationStarter.Add(new XElement("Cert"));
                elementCert = elementIntegrationStarter.Element("Cert");
            }

            if (elementCert != null)
            {
                if (save)
                {
                    elementCert.Value = txtCertificateName.Text;
                }
                else
                {
                    txtCertificateName.Text = elementCert.Value;
                    txtCertificateName.Tag = txtCertificateName.Text;
                }
            }

            #endregion

            #region TreservaArchiveIntegrationService.

            XElement elementTreservaArchiveIntegrationService = 
                XmlHelper.GetIntegrationStarterChildNodes(elementIntegrationStarter, "TreservaArchiveIntegrationService");

            XAttribute attributeInstallHistorik = elementTreservaArchiveIntegrationService.Attribute("Install");
            if (attributeInstallHistorik == null)
            {
                elementTreservaArchiveIntegrationService.Add(new XAttribute("Install", "false"));
                attributeInstallHistorik = elementTreservaArchiveIntegrationService.Attribute("Install");
            }

            XElement elementHistorikHost = elementTreservaArchiveIntegrationService.Element("HttpsBaseHost");
            if (elementHistorikHost == null)
            {
                elementTreservaArchiveIntegrationService.Add(new XElement("HttpsBaseHost"));
                elementHistorikHost = elementTreservaArchiveIntegrationService.Element("HttpsBaseHost");
            }

            XElement elementHistorikPort = elementTreservaArchiveIntegrationService.Element("HttpsBasePort");
            if (elementHistorikPort == null)
            {
                elementTreservaArchiveIntegrationService.Add(new XElement("HttpsBasePort"));
                elementHistorikPort = elementTreservaArchiveIntegrationService.Element("HttpsBasePort");
            }

            if (attributeInstallHistorik != null)
            {
                if (save)
                {
                    attributeInstallHistorik.Value = chkInstallHistorik.Checked.ToString().ToLower();
                }
                else
                {
                    chkInstallHistorik.Checked = attributeInstallHistorik.Value == "true";
                    chkInstallHistorik.Tag = chkInstallHistorik.Checked;
                }
            }

            if (elementHistorikHost != null)
            {
                if (save)
                {
                    elementHistorikHost.Value = txtHistorikHost.Text;
                }
                else
                {
                    txtHistorikHost.Text = elementHistorikHost.Value;
                    txtHistorikHost.Tag = txtHistorikHost.Text;
                }
            }

            if (elementHistorikPort != null)
            {
                if (save)
                {
                    elementHistorikPort.Value = txtHistorikPort.Text;
                }
                else
                {
                    txtHistorikPort.Text = elementHistorikPort.Value;
                    txtHistorikPort.Tag = txtHistorikPort.Text;
                }
            }

            #endregion

            #region TreservaCareIntegrationService.

            XElement elementTreservaCareIntegrationService = 
                XmlHelper.GetIntegrationStarterChildNodes(elementIntegrationStarter, "TreservaCareIntegrationService");

            XAttribute attributeInstallUtfordTid = elementTreservaCareIntegrationService.Attribute("Install");
            if (attributeInstallUtfordTid == null)
            {
                elementTreservaCareIntegrationService.Add(new XAttribute("Install", "false"));
                attributeInstallUtfordTid = elementTreservaCareIntegrationService.Attribute("Install");
            }

            XElement elementUtfordTidHost = elementTreservaCareIntegrationService.Element("HttpBaseHost");
            if (elementUtfordTidHost == null)
            {
                elementTreservaCareIntegrationService.Add(new XElement("HttpBaseHost"));
                elementUtfordTidHost = elementTreservaCareIntegrationService.Element("HttpBaseHost");
            }

            XElement elementUtfordTidPort = elementTreservaCareIntegrationService.Element("HttpBasePort");
            if (elementUtfordTidPort == null)
            {
                elementTreservaCareIntegrationService.Add(new XElement("HttpBasePort"));
                elementUtfordTidPort = elementTreservaCareIntegrationService.Element("HttpBasePort");
            }

            if (attributeInstallUtfordTid != null)
            {
                if (save)
                {
                    attributeInstallUtfordTid.Value = chkInstallUtfordTid.Checked.ToString().ToLower();
                }
                else
                {
                    chkInstallUtfordTid.Checked = attributeInstallUtfordTid.Value == "true";
                    chkInstallUtfordTid.Tag = chkInstallUtfordTid.Checked;
                }
            }

            if (elementUtfordTidHost != null)
            {
                if (save)
                {
                    elementUtfordTidHost.Value = txtUtfordTidHost.Text;
                }
                else
                {
                    txtUtfordTidHost.Text = elementUtfordTidHost.Value;
                    txtUtfordTidHost.Tag = txtUtfordTidHost.Text;
                }
            }

            if (elementUtfordTidPort != null)
            {
                if (save)
                {
                    elementUtfordTidPort.Value = txtUtfordTidPort.Text;
                }
                else
                {
                    txtUtfordTidPort.Text = elementUtfordTidPort.Value;
                    txtUtfordTidPort.Tag = txtUtfordTidPort.Text;
                }
            }

            #endregion

            #region TreservaEServiceIntegrationService.

            XElement elementTreservaEServiceIntegrationService = 
                XmlHelper.GetIntegrationStarterChildNodes(elementIntegrationStarter, "TreservaEServiceIntegrationService");

            XAttribute attributeInstallEServices = elementTreservaEServiceIntegrationService.Attribute("Install");
            if (attributeInstallEServices == null)
            {
                elementTreservaEServiceIntegrationService.Add(new XAttribute("Install", "false"));
                attributeInstallEServices = elementTreservaEServiceIntegrationService.Attribute("Install");
            }

            XElement elementEservicesHost = elementTreservaEServiceIntegrationService.Element("HttpBaseHost");
            if (elementEservicesHost == null)
            {
                elementTreservaEServiceIntegrationService.Add(new XElement("HttpBaseHost"));
                elementEservicesHost = elementTreservaEServiceIntegrationService.Element("HttpBaseHost");
            }

            XElement elementEServicesPort = elementTreservaEServiceIntegrationService.Element("HttpBasePort");
            if (elementEServicesPort == null)
            {
                elementTreservaEServiceIntegrationService.Add(new XElement("HttpBasePort"));
                elementEServicesPort = elementTreservaEServiceIntegrationService.Element("HttpBasePort");
            }

            if (attributeInstallEServices != null)
            {
                if (save)
                {
                    attributeInstallEServices.Value = chkInstallEServices.Checked.ToString().ToLower();
                }
                else
                {
                    chkInstallEServices.Checked = attributeInstallEServices.Value == "true";
                    chkInstallEServices.Tag = chkInstallEServices.Checked;
                }
            }

            if (elementEservicesHost != null)
            {
                if (save)
                {
                    elementEservicesHost.Value = txtEServicesHost.Text;
                }
                else
                {
                    txtEServicesHost.Text = elementEservicesHost.Value;
                    txtEServicesHost.Tag = txtEServicesHost.Text;
                }
            }

            if (elementEServicesPort != null)
            {
                if (save)
                {
                    elementEServicesPort.Value = txtEServicesPort.Text;
                }
                else
                {
                    txtEServicesPort.Text = elementEServicesPort.Value;
                    txtEServicesPort.Tag = txtEServicesPort.Text;
                }
            }

            #endregion

            #region TreservaTGPIntegrationService.

            XElement elementTreservaTGPIntegrationService = 
                XmlHelper.GetIntegrationStarterChildNodes(elementIntegrationStarter, "TreservaTGPIntegrationService");

            XAttribute attributeInstallTgp = elementTreservaTGPIntegrationService.Attribute("Install");
            if (attributeInstallTgp == null)
            {
                elementTreservaTGPIntegrationService.Add(new XAttribute("Install", "false"));
                attributeInstallTgp = elementTreservaTGPIntegrationService.Attribute("Install");
            }

            XElement elementTgpHost = elementTreservaTGPIntegrationService.Element("HttpsBaseHost");
            if (elementTgpHost == null)
            {
                elementTreservaTGPIntegrationService.Add(new XElement("HttpsBaseHost"));
                elementTgpHost = elementTreservaTGPIntegrationService.Element("HttpsBaseHost");
            }

            XElement elementTgpPort = elementTreservaTGPIntegrationService.Element("HttpsBasePort");
            if (elementTgpPort == null)
            {
                elementTreservaTGPIntegrationService.Add(new XElement("HttpsBasePort"));
                elementTgpPort = elementTreservaTGPIntegrationService.Element("HttpsBasePort");
            }

            XElement elementTgpCert = elementTreservaTGPIntegrationService.Element("Cert");
            if (elementTgpCert == null)
            {
                elementTreservaTGPIntegrationService.Add(new XElement("Cert"));
                elementTgpCert = elementTreservaTGPIntegrationService.Element("Cert");
            }

            if (attributeInstallTgp != null)
            {
                if (save)
                {
                    attributeInstallTgp.Value = chkInstallTgp.Checked.ToString().ToLower();
                }
                else
                {
                    chkInstallTgp.Checked = attributeInstallTgp.Value == "true";
                    chkInstallTgp.Tag = chkInstallTgp.Checked;
                }
            }

            if (elementTgpHost != null)
            {
                if (save)
                {
                    elementTgpHost.Value = txtTgpHost.Text;
                }
                else
                {
                    txtTgpHost.Text = elementTgpHost.Value;
                    txtTgpHost.Tag = txtTgpHost.Text;
                }
            }

            if (elementTgpPort != null)
            {
                if (save)
                {
                    elementTgpPort.Value = txtTgpPort.Text;
                }
                else
                {
                    txtTgpPort.Text = elementTgpPort.Value;
                    txtTgpPort.Tag = txtTgpPort.Text;
                }
            }

            if (elementTgpCert != null)
            {
                if (save)
                {
                    elementTgpCert.Value = txtTgpCertificateName.Text;
                }
                else
                {
                    txtTgpCertificateName.Text = elementTgpCert.Value;
                    txtTgpCertificateName.Tag = txtTgpCertificateName.Text;
                }
            }

            #endregion
        }

        #endregion

        #region EditOutboundServices.

        private void EditOutboundServices(bool save)
        {
            XElement elementIntegration = XmlHelper.GetApplicationChildNodes(_application, "Integration");
            XElement elementIntegrationStarter = XmlHelper.GetIntegrationChildNodes(elementIntegration, "IntegrationStarter");

            #region IntegrationStarterTES.

            XElement elementIntegrationStarterTES = XmlHelper.GetIntegrationStarterChildNodes(elementIntegrationStarter, "IntegrationStarterTES");

            XAttribute attributeTesInstall = elementIntegrationStarterTES.Attribute("Install");
            if (attributeTesInstall == null)
            {
                elementIntegrationStarterTES.Add(new XAttribute("Install", "false"));
                attributeTesInstall = elementIntegrationStarterTES.Attribute("Install");
            }

            XElement elementTesEnpointHost = elementIntegrationStarterTES.Element("TESEndpointHost");
            if (elementTesEnpointHost == null)
            {
                elementIntegrationStarterTES.Add(new XElement("TESEndpointHost"));
                elementTesEnpointHost = elementIntegrationStarterTES.Element("TESEndpointHost");
            }

            XElement elementTesEnpointPort = elementIntegrationStarterTES.Element("TESEndpointPort");
            if (elementTesEnpointPort == null)
            {
                elementIntegrationStarterTES.Add(new XElement("TESEndpointPort"));
                elementTesEnpointPort = elementIntegrationStarterTES.Element("TESEndpointPort");
            }

            if (attributeTesInstall != null)
            {
                if (save)
                {
                    attributeTesInstall.Value = chkInstallTes.Checked.ToString().ToLower();
                }
                else
                {
                    chkInstallTes.Checked = attributeTesInstall.Value == "true";
                    chkInstallTes.Tag = chkInstallTes.Checked;
                }
            }

            if (elementTesEnpointHost != null)
            {
                if (save)
                {
                    elementTesEnpointHost.Value = txtTesServiceHost.Text;
                }
                else
                {
                    txtTesServiceHost.Text = elementTesEnpointHost.Value;
                    txtTesServiceHost.Tag = txtTesServiceHost.Text;
                }
            }

            if (elementTesEnpointPort != null)
            {
                if (save)
                {
                    elementTesEnpointPort.Value = txtTesServicePort.Text;
                }
                else
                {
                    txtTesServicePort.Text = elementTesEnpointPort.Value;
                    txtTesServicePort.Tag = txtTesServicePort.Text;
                }
            }

            #endregion

            #region IntegrationStarterMobipen.

            XElement elementIntegrationStarterMobipen = XmlHelper.GetIntegrationStarterChildNodes(elementIntegrationStarter, "IntegrationStarterMobipen");

            XAttribute attributeMobipenInstall = elementIntegrationStarterMobipen.Attribute("Install");
            if (attributeMobipenInstall == null)
            {
                elementIntegrationStarterMobipen.Add(new XAttribute("Install", "false"));
                attributeMobipenInstall = elementIntegrationStarterMobipen.Attribute("Install");
            }

            XElement elementMobipenHost = elementIntegrationStarterMobipen.Element("MobipenEndpointHost");
            if (elementMobipenHost == null)
            {
                elementIntegrationStarterMobipen.Add(new XElement("MobipenEndpointHost"));
                elementMobipenHost = elementIntegrationStarterMobipen.Element("MobipenEndpointHost");
            }

            XElement elementMobipenPort = elementIntegrationStarterMobipen.Element("MobipenEndpointPort");
            if (elementMobipenPort == null)
            {
                elementIntegrationStarterMobipen.Add(new XElement("MobipenEndpointPort"));
                elementMobipenPort = elementIntegrationStarterMobipen.Element("MobipenEndpointPort");
            }

            if (attributeMobipenInstall != null)
            {
                if (save)
                {
                    attributeMobipenInstall.Value = chkInstallMobiPen.Checked.ToString().ToLower();
                }
                else
                {
                    chkInstallMobiPen.Checked = attributeMobipenInstall.Value == "true";
                    chkInstallMobiPen.Tag = chkInstallMobiPen.Checked;
                }
            }

            if (elementMobipenHost != null)
            {
                if (save)
                {
                    elementMobipenHost.Value = txtMobipenEndpointHost.Text;
                }
                else
                {
                    txtMobipenEndpointHost.Text = elementMobipenHost.Value;
                    txtMobipenEndpointHost.Tag = txtMobipenEndpointHost.Text;
                }
            }

            if (elementMobipenPort != null)
            {
                if (save)
                {
                    elementMobipenPort.Value = txtMobipenEndpointPort.Text;
                }
                else
                {
                    txtMobipenEndpointPort.Text = elementMobipenPort.Value;
                    txtMobipenEndpointPort.Tag = txtMobipenEndpointPort.Text;
                }
            }

            #endregion

            #region IntegrationStarterCardEmbossing.

            XElement elementIntegrationStarterCardEmbossing = XmlHelper.GetIntegrationStarterChildNodes(elementIntegrationStarter, "IntegrationStarterCardEmbossing");

            XAttribute attributeCardEmbossingInstall = elementIntegrationStarterCardEmbossing.Attribute("Install");
            if (attributeCardEmbossingInstall == null)
            {
                elementIntegrationStarterCardEmbossing.Add(new XAttribute("Install", "false"));
                attributeCardEmbossingInstall = elementIntegrationStarterCardEmbossing.Attribute("Install");
            }

            XElement elementCardEmbossingFilePath = elementIntegrationStarterCardEmbossing.Element("CardEmbossingFilePath");
            if (elementCardEmbossingFilePath == null)
            {
                elementIntegrationStarterCardEmbossing.Add(new XElement("CardEmbossingFilePath"));
                elementCardEmbossingFilePath = elementIntegrationStarterCardEmbossing.Element("CardEmbossingFilePath");
            }

            if (attributeCardEmbossingInstall != null)
            {
                if (save)
                {
                    attributeCardEmbossingInstall.Value = chkInstallKortpragling.Checked.ToString().ToLower();
                }
                else
                {
                    chkInstallKortpragling.Checked = attributeCardEmbossingInstall.Value == "true";
                    chkInstallKortpragling.Tag = chkInstallKortpragling.Checked;
                }
            }

            if (elementCardEmbossingFilePath != null)
            {
                if (save)
                {
                    elementCardEmbossingFilePath.Value = txtKortpraglingPath.Text;
                }
                else
                {
                    txtKortpraglingPath.Text = elementCardEmbossingFilePath.Value;
                    txtKortpraglingPath.Tag = txtKortpraglingPath.Text;
                }
            }

            #endregion

            #region IntegrationStarterHealthcare.

            XElement elementIntegrationStarterHealthcare = XmlHelper.GetIntegrationStarterChildNodes(elementIntegrationStarter, "IntegrationStarterHealthcare");

            XAttribute attributeHealthCareInstall = elementIntegrationStarterHealthcare.Attribute("Install");
            if (attributeHealthCareInstall == null)
            {
                elementIntegrationStarterHealthcare.Add(new XAttribute("Install", "false"));
                attributeHealthCareInstall = elementIntegrationStarterHealthcare.Attribute("Install");
            }

            XElement elementHealthCareProfessional = elementIntegrationStarterHealthcare.Element("HealthcareProfessionalAddress");
            if (elementHealthCareProfessional == null)
            {
                elementIntegrationStarterHealthcare.Add(new XElement("HealthcareProfessionalAddress"));
                elementHealthCareProfessional = elementIntegrationStarterHealthcare.Element("HealthcareProfessionalAddress");
            }

            XElement elementHealtCareProvider = elementIntegrationStarterHealthcare.Element("HealthcareProviderAddress");
            if (elementHealtCareProvider == null)
            {
                elementIntegrationStarterHealthcare.Add(new XElement("HealthcareProviderAddress"));
                elementHealtCareProvider = elementIntegrationStarterHealthcare.Element("HealthcareProviderAddress");
            }

            if (attributeHealthCareInstall != null)
            {
                if (save)
                {
                    attributeHealthCareInstall.Value = chkInstallLakemedel.Checked.ToString().ToLower();
                }
                else
                {
                    chkInstallLakemedel.Checked = attributeHealthCareInstall.Value == "true";
                    chkInstallLakemedel.Tag = chkInstallLakemedel.Checked;
                }
            }

            if (elementHealthCareProfessional != null)
            {
                if (save)
                {
                    elementHealthCareProfessional.Value = txtProfessionalAddress.Text;
                }
                else
                {
                    txtProfessionalAddress.Text = elementHealthCareProfessional.Value;
                    txtProfessionalAddress.Tag = txtProfessionalAddress.Text;
                }
            }

            if (elementHealtCareProvider != null)
            {
                if (save)
                {
                    elementHealtCareProvider.Value = txtProviderAddress.Text;
                }
                else
                {
                    txtProviderAddress.Text = elementHealtCareProvider.Value;
                    txtProviderAddress.Tag = txtProviderAddress.Text;
                }
            }

            #endregion

            #region IntegrationStarterRefugee.

            XElement elementIntegrationStarterRefugee = XmlHelper.GetIntegrationStarterChildNodes(elementIntegrationStarter, "IntegrationStarterRefugee");

            XAttribute attributeRefugeeInstall = elementIntegrationStarterRefugee.Attribute("Install");
            if (attributeRefugeeInstall == null)
            {
                elementIntegrationStarterRefugee.Add(new XAttribute("Install", "false"));
                attributeRefugeeInstall = elementIntegrationStarterRefugee.Attribute("Install");
            }

            if (attributeRefugeeInstall != null)
            {
                if (save)
                {
                    attributeRefugeeInstall.Value = chkInstallFlykting.Checked.ToString().ToLower();
                }
                else
                {
                    chkInstallFlykting.Checked = attributeRefugeeInstall.Value == "true";
                    chkInstallFlykting.Tag = chkInstallFlykting.Checked;
                }
            }

            #endregion
        }

        #endregion
    }
}
